//
//  SSSMyMemberViewController.m
//  SSSMerchant
//
//  Created by Darker on 2017/3/25.
//  Copyright © 2017年 xnxin. All rights reserved.
//

#import "SSSMyMemberViewController.h"
#import "SSSMyMemberCell.h"
#import "SSSMyMemberModel.h"

@interface SSSMyMemberViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_listTableView;
    NSInteger _currentPage;//当前页
    NSMutableArray *_dataArray;
}

///** <#注释#> */
//@property (nonatomic, strong) NSMutableArray *dataArray;

@end

static NSString *MyMemberID    = @"MyMemberID";

@implementation SSSMyMemberViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.title = @"我 的 会 员";
    self.view.backgroundColor = [UIColor colorWithHex:@"F9F9FA"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setApperance];
    
    // 加载数据
    [self loadData];

}
- (void)setApperance{
    
    UIView *headerView = [UIView new];
    headerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:headerView];
    headerView.layer.borderWidth = 0.5;
    headerView.layer.borderColor = [UIColor colorWithHex:@"E5E5E8"].CGColor;
    headerView.sd_layout
    .topSpaceToView(self.view,HeigthIos6CompeteWithCurrent(5)+64)
    .leftEqualToView(self.view)
    .rightEqualToView(self.view)
    .heightIs(HeigthIos6CompeteWithCurrent(50));
    
    NSArray *titleArray = @[@"姓名",@"手机号",@"加入日期"];
    CGFloat width = KSCREEN_Width/3;
    CGFloat height = HeigthIos6CompeteWithCurrent(50);
    for (int index = 0; index < titleArray.count; index++) {
        
        UILabel *nameLabel = [UILabel new];
        nameLabel.text = titleArray[index];
        nameLabel.textColor = [UIColor colorWithHex:@"6D6D6D"];
        nameLabel.font = [SSSFont systemFontOfSize:15];
        nameLabel.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:nameLabel];
        nameLabel.frame = CGRectMake(index * width, 0, width, height);
        
        
        if (index == 0) {
            UIView *lineView = [UIView new];
            lineView.backgroundColor = [UIColor colorWithHex:@"E5E5E8"];
            [nameLabel addSubview:lineView];
            lineView.sd_layout
            .topSpaceToView(nameLabel,HeigthIos6CompeteWithCurrent(7))
            .bottomSpaceToView(nameLabel,HeigthIos6CompeteWithCurrent(7))
            .rightSpaceToView(nameLabel,0)
            .widthIs(1);
        }else if (index == 1){
            
            UIView *lineView = [UIView new];
            lineView.backgroundColor = [UIColor colorWithHex:@"E5E5E8"];
            [nameLabel addSubview:lineView];
            lineView.sd_layout
            .topSpaceToView(nameLabel,HeigthIos6CompeteWithCurrent(7))
            .bottomSpaceToView(nameLabel,HeigthIos6CompeteWithCurrent(7))
            .rightSpaceToView(nameLabel,0)
            .widthIs(1);

        }
        
    }
    
    
    _listTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64 + HeigthIos6CompeteWithCurrent(5)+HeigthIos6CompeteWithCurrent(50), KSCREEN_Width, KSCREEN_Height - (64+ HeigthIos6CompeteWithCurrent(5)+HeigthIos6CompeteWithCurrent(50))) style:UITableViewStylePlain];
    _listTableView.delegate = self;
    _listTableView.dataSource = self;
    _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _listTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_listTableView];
    _listTableView.rowHeight = HeigthIos6CompeteWithCurrent(50);
    //_listTableView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
    
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Class currentClass = [SSSMyMemberCell class];
    
    SSSMyMemberCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(currentClass)];
    if (!cell) {
        cell = [[SSSMyMemberCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyMemberID];
    }
    cell.myMemberModel = _dataArray[indexPath.row];
    return cell;
    
    
}
- (void)loadData{
    
    ZDRefreshHeader *header = [ZDRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 马上进入刷新状态
    [header beginRefreshing];
    // 设置header
    _listTableView.mj_header = header;
    
    // 上拉加载
    _listTableView.mj_footer = [ZDAutofooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];

    
}
- (void)loadNewData{
 
    _currentPage = 1;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"currentPage"] = @(_currentPage);
    params[@"pageSize"] = @"20";
    
    [[XHRequestManager manager] getRequest:kConfigPath(kMyMember) params:params

    success:^(id  _Nonnull responseObj) {
        
        XHLog(@"-----%@",responseObj);
        if ([responseObj[@"status"] intValue] == 0){
            
            [_dataArray removeAllObjects];
            
            if ([responseObj[@"data"][@"pageList"] count] == 0) {
                
                showTipHudStr(@"该数据为空");
                [_listTableView.mj_header endRefreshing];
                _listTableView.mj_footer.hidden = YES;
                return ;
            }
            
            NSArray *array = [SSSMyMemberModel mj_objectArrayWithKeyValuesArray:responseObj[@"data"][@"pageList"]];
            _dataArray = [NSMutableArray array];
            [_dataArray addObjectsFromArray:array];
            
            //回到主线程刷新表格
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //刷新表格
                [_listTableView reloadData];
                
            });
            
            [_listTableView.mj_header endRefreshing];
            
        }else{
            
            showTipHudStr(kResponseMessage);
            [_listTableView.mj_header endRefreshing];
        }

        
    } failure:^(NSError * _Nonnull error) {
        
        XHLog(@"---error----%@",error.localizedDescription);
        showTipHudStr(error.localizedDescription);
        [_listTableView.mj_header endRefreshing];
        [_listTableView.mj_footer endRefreshing];
        
    }];
    
}
- (void)loadMoreData{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"currentPage"] = @(++_currentPage);
    params[@"pageSize"] = @"20";
    
    [[XHRequestManager manager] getRequest:kConfigPath(kMyMember) params:params success:^(id  _Nonnull responseObj) {
        
        XHLog(@"-----%@",responseObj);
        if ([responseObj[@"status"] intValue] == 0){
            
            NSArray *array = [SSSMyMemberModel mj_objectArrayWithKeyValuesArray:responseObj[@"data"][@"pageList"]];
            
            [_dataArray addObjectsFromArray:array];
            //NSDictionary *dict = @{@"allMoney":response[@"data"][@"totalMoney"],@"rangli":response[@"data"][@"totalRangli"]};
            
            //回到主线程刷新表格
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //刷新表格
                [_listTableView reloadData];
                
            });
            
            _listTableView.mj_footer.hidden = (_currentPage == 1);
            
            if ([responseObj[@"data"][@"page"][@"hasNextPage"] intValue] == 0) {
                
                
                [_listTableView.mj_footer endRefreshingWithNoMoreData];
                
            }else{
                
                showTipHudStr(kResponseMessage);
                [_listTableView.mj_header endRefreshing];
            }
            
        }else{
            
            showTipHudStr(kResponseMessage);
            [_listTableView.mj_header endRefreshing];
        }

        
    } failure:^(NSError * _Nonnull error) {
        
        XHLog(@"---error----%@",error.localizedDescription);
        showTipHudStr(error.localizedDescription);
        [_listTableView.mj_header endRefreshing];
        [_listTableView.mj_footer endRefreshing];
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSMutableArray *)dataArray{
    
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
